/**
 * author: weiki
 * date: 2017/09/28
 * pages/invite/invite.js
 * 
 */

var app = getApp();
var cutTime;
Page({

	data: {
		num: 0,//还差的人数
		milliseconds: 1000,//倒计时的毫秒数
		countdown: '',        //倒计时
		state: 1,            //拼团状态
		success: true,        //拼团成功
		orderInfo: {},         //订单信息
		urlImage: app.urlimg,
		orderId: 0,
		goodsId: 0,
		readFinish: false,
		hint: "",
		teamSign: 0,
		isPrize: false
	},

	/**
	 * 点击商品跳转到详情页
	 */
	goodsDetail: function () {
		var groupType = this.data.orderInfo.groupType;
		var regimentId = this.data.orderInfo.orderId;
		var goodsId = this.data.goodsId;
		wx.navigateTo({
			url: '/pages/group/groupdetail/index?id=' + goodsId +
			"&regiment_id=" + regimentId + "&group_type=" + groupType,
		})
	},
	//查看规则
	go_rule: function () {
		wx.navigateTo({
			url: '/pages/group/rule/index',
			success: function () {
				clearInterval(cutTime);
			},
		})
	},

	/**
	 * 重新开团
	 */
	renew: function () {
		var that = this;
		var regimentId = that.data.orderInfo.regimentId;
		var groupType = that.data.orderInfo.groupType;
		wx.redirectTo({
			url: '/pages/group/groupdetail/index?id=' + that.data.goodsId + "&regiment_id="
			+ regimentId + "&group_type=" + groupType,
		})
	},




	// 获得毫秒数  转化  成小时、分钟、秒
	time: function (Time) {
		var that = this;
		Time -= 1000;
		if (Time > 0) {
			var hours = parseInt(Time / 1000 / 60 / 60 % 24, 10); //计算剩余的小时 
			if (('' + hours).length == 1) {
				hours = '0' + hours;
			}
			var minutes = parseInt(Time / 1000 / 60 % 60, 10);//计算剩余的分钟 
			if (('' + minutes).length == 1) {
				minutes = '0' + minutes;
			}
			var seconds = parseInt(Time / 1000 % 60, 10);//计算剩余的秒数 
			if (('' + seconds).length == 1) {
				seconds = '0' + seconds;
			}
			var times = hours + ":" + minutes + ":" + seconds;//排列时间

			// console.log(times);
			that.setData({
				countdown: times,
				milliseconds: Time,
			})
		}
	},
	// 进入商品详情页面(去开团)
	click: function (e) {
		var that = this;
		console.log(e.currentTarget.dataset);
		wx.redirectTo({
			url: '/pages/group/groupdetail/index?id=' + e.currentTarget.dataset.index + '& regiment_id=' + e.currentTarget.dataset.regiment_id + '& group_type=' + e.currentTarget.dataset.group,
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		var isPrize = options.isPrize == 1 ? true : false;
		//获取拼团数据
		var that = this;

		var token_key = wx.getStorageSync('token_key');
		console.log(token_key);
		if (token_key == undefined || token_key == '') {
			app.getUserInfo(function () {
				load_fn();
			})
		} else {
			load_fn()
		}

		function load_fn() {
			wx.request({
				url: app.url + "api_team/inviteGroup",
				data: {
					orderId: options.orderId
				},
				header: {
					'content-type': 'application/json',
					'token': wx.getStorageSync('token_key')
				},
				success: function (res) {
					console.log(res.data.result);
					var phone = res.data.result.mobile.trim();
					res.data.result.telphone = phone.replace(/(\d{3})(\d{4})(\d{4})/, "$1****$3");//不显示完整号码
					var status = parseInt(res.data.result.teamStatus);
					var title = '';
					console.log(status)
					var member = res.data.result.member;
					var orderId = res.data.result.currentUser;

					var user_look = false;
					for (var i = 0; i < member.length; i++) {
						if (member[i].user_id == orderId) {
							user_look = true;
						}
					}
					if (user_look) {//参团中有用户自身时
						switch (status) {
							case 1:
								title = '拼团中，快叫小伙伴一起来拼团吧~';
								break;
							case 2:
								title = '恭喜你，拼团成功，等待抽奖';
								break;
							case 3:
								title = '待收货';
								break;
							default:
								title = '未在规定时间内未完成拼团';
								break;
						}
					} else {//参团中没有用户自身时
						switch (status) {
							case 1:
								title = '拼团中，快叫小伙伴一起来拼团吧~';
								break;
							case 2:
								title = '该团已拼团成功';
								break;
							case 3:
								title = '该团已拼团成功';
								break;
							default:
								title = '该团未在规定时间内未完成拼团';
								break;
						}
					}
					that.setData({
						isPrize: isPrize,
						readFinish: true,
						orderInfo: res.data.result,
						num: res.data.result.needPeople,
						milliseconds: res.data.result.surplusTime,
						state: res.data.result.teamStatus,
						hint: title,
						teamSign: res.data.result.teamSign,
						goodsId: res.data.result.goods[0].goods_id,
						orderId: res.data.result.orderId
					});
				}
			})

			//拼团推荐
			wx.request({
				url: app.url + 'api_team/recommend',
				data: {
					// goods_id: o.id,
					goods_id: 2829,
				},
				header: {
					'Content-Type': 'application/json',
					'token': wx.getStorageSync('token_key')
				},
				success: function (res) {
					console.log(res.data.result);
					that.setData({
						goods_lists: res.data.result,
					})

				},
				// 接口调用失败返回的函数
				fail: function (err) {
					console.log(err)
				}
			})
		}
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		var that = this;
		if (that.data.state == 1) {
			cutTime = setInterval(function () {
				if (that.data.milliseconds >= 1000) {
					that.time(that.data.milliseconds -= 1);
				} else {
					that.setData({
						state: 5
					});
					clearInterval(cutTime);
				}
			}, 1000);
		}
	},
	onHide: function () {
		clearInterval(cutTime);
	},
	onUnload: function () {
		clearInterval(cutTime);
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () { },

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () { },

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () { },

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () { },

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () { },

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function (res) {

		var orderId = this.data.orderId;
		var teamSign = this.data.teamSign;
		var regimentId = this.data.orderInfo.regimentId;
		var goodsId = this.data.goodsId;
		var shareText = this.data.orderInfo.goods[0].shareText;
		return {
			title: "我正在抢" + shareText + "，要不要一起来玩呢?",
			path: '/pages/group/group_of/group_of?id=' + goodsId + '&order_id=' + orderId + '&teamSign=' + teamSign + '&regimentId=' + regimentId,
			success: function (res) {
				clearInterval(cutTime);
			},
			fail: function (res) {
				clearInterval(cutTime);
			}
		}
	},

	autoSubmit: function (e) {
		wx.request({
			url: app.url + "api_team/collectFormId",
			data: {
				formId: e.detail.formId
			},
			header: {
				'content-type': 'application/json',
				'token': wx.getStorageSync('token_key')
			},
			success: function (result) { }
		})
	}
});